﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;

namespace Eflong.DataAccess
{
    public static class DatabaseHelper
    {
        public const string ConnectionStringName = "EflongDataAccess.Properties.Settings.KWSystemConnectionString";
        public const string ApplicationID = "fd639154-299a-4a9d-1234-69dc28eb6388";
        public readonly static Guid ApplicationGuid = new Guid( ApplicationID );

        private static string connectionString = null;

        public static string GetConnectString( )
        {
            if ( ConfigurationManager.ConnectionStrings[ ConnectionStringName ] != null )
            {
                return ConfigurationManager.ConnectionStrings[ ConnectionStringName ].ConnectionString;
            }
            else if ( ConfigurationManager.ConnectionStrings[ "KWSystemConnectionString" ] != null )
            {
                return ConfigurationManager.ConnectionStrings[ "KWSystemConnectionString" ].ConnectionString;
            }
            else
            {
                return ConfigurationManager.ConnectionStrings[ 1 ].ConnectionString;
            }
        }

        public static KWSystemDBDataContext GetKWSystemDBDataContext( )
        {
            if ( connectionString == null )
            {
                connectionString = GetConnectString( );
            }

            if ( !connectionString.Contains( "User ID=kwuser;Password=1234" ) )
            {
                connectionString += "User ID=kwuser;Password=1234";
            }

            var db = new KWSystemDBDataContext( connectionString );
            db.CommandTimeout = 60 * 3; // 超时时间修改为 60 秒, 默认为 30 秒

            return db;
        }

        public static KWSystemDBDataContext GetKWSystemDBDataContext( string _connectionString )
        {
            connectionString = _connectionString;
            var db = new KWSystemDBDataContext( connectionString );
            db.CommandTimeout = 60 * 3; // 超时时间修改为 60 秒, 默认为 30 秒

            return db;
        }

        public static void Update<T>( T obj , Action<T> update ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                db.GetTable<T>( ).Attach( obj );
                update( obj );
                try
                {
                    db.SubmitChanges( ConflictMode.ContinueOnConflict );
                }
                catch ( System.Data.Linq.ChangeConflictException )
                {
                    foreach ( System.Data.Linq.ObjectChangeConflict occ in db.ChangeConflicts )
                    {
                        //以下是解决冲突的三种方法，选一种即可

                        // 使用当前数据库中的值，覆盖Linq缓存中实体对象的值
                        //occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

                        // 使用Linq缓存中实体对象的值，覆盖当前数据库中的值
                        occ.Resolve( System.Data.Linq.RefreshMode.KeepCurrentValues );

                        // 只更新实体对象中改变的字段的值，其他的保留不变
                        //occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
                    }

                    // 这个地方要注意，Catch方法中，我们前面只是指明了怎样来解决冲突，这个地方还需要再次提交更新，这样的话，值    //才会提交到数据库。
                    db.SubmitChanges( );
                }
            }
        }

        public static void UpdateAll<T>( List<T> items , Action<T> update ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                Table<T> table = db.GetTable<T>( );
                foreach ( T item in items )
                {
                    table.Attach( item );
                    update( item );
                }

                db.SubmitChanges( );
            }
        }

        public static void Delete<T>( T entity ) where T : class, new( )
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                Table<T> table = db.GetTable<T>( );
                table.Attach( entity );
                table.DeleteOnSubmit( entity );

                db.SubmitChanges( );
            }
        }

        public static void DeleteAll<T>( List<T> items ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                Table<T> table = db.GetTable<T>( );
                foreach ( T item in items )
                {
                    table.Attach( item );
                    table.DeleteOnSubmit( item );
                }

                db.SubmitChanges( );
            }
        }

        public static void InsertAll<T>( IEnumerable<T> items ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                db.GetTable<T>( ).InsertAllOnSubmit( items );
                db.SubmitChanges( );
            }
        }

        public static void InsertAll<T>( List<T> items ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                db.GetTable<T>( ).InsertAllOnSubmit( items );
                db.SubmitChanges( );
            }

            //转储数据等待同步到联网服务器
            HisAlarmDataDump hisAlarmDataDump = new HisAlarmDataDump( );
            hisAlarmDataDump.Save<T>( items );
        }

        public static void Insert<T>( T obj ) where T : class
        {
            using ( var db = GetKWSystemDBDataContext( ) )
            {
                db.GetTable<T>( ).InsertOnSubmit( obj );

                db.SubmitChanges( );
            }
        }

        public static void InsertForHisData<T>( T obj , string connString ) where T : class
        {
            using ( var db = new KWSystem_HisDataDBDataContext( connString ) )
            {
                db.GetTable<T>( ).InsertOnSubmit( obj );
                db.SubmitChanges( );
            }
        }
    }
}